package com.winsun.hellozookeeper.client;

import com.winsun.hellozookeeper.util.Constants;
import org.apache.zookeeper.ZooKeeper;

import java.util.Collections;
import java.util.List;
import java.util.stream.IntStream;

public class ZkClient {
    static ZooKeeper zk;
    static {
        try{
            zk=new ZooKeeper(Constants.ZK_URL,Constants.TIME_OUT,null);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static List<String> getServerList(){
        List<String> serverList=null;
        try {
            serverList=zk.getChildren(Constants.ZK_ROOT,false);
        }catch (Exception e){
            e.printStackTrace();
        }
        return serverList;
    }
    public static void handle(){
        List<String>serverList=getServerList();
        //打乱
        Collections.shuffle(serverList);
        //随机拿一个server
        new Client(serverList.get(0));
    }

    public static void main(String[] args) {
        IntStream.range(1,10).forEach(value -> {
            handle();
            try{
                Thread.sleep(1000);
            }catch (Exception e){
                e.printStackTrace();
            }

        });
    }
}
